#include <assert.h>
#include <irrlicht.h>
#include "demo.h"
#include "fx_kuksa.h"


using namespace irr;
using namespace irr::core;
using namespace irr::scene;


static ISceneManager *sSceneManager = 0;
static ISceneNode *sKuksa = 0;


bool KuksaFx::init()
{
    if (!gIrrDevice || !gIrrDevice->getSceneManager())
        return false;

    //bool zipRes = gIrrDevice->getFileSystem()->addFileArchive("data_oldies_kuksa.zip", false, false, io::EFAT_ZIP);
    //assert(zipRes);

    sSceneManager = gIrrDevice->getSceneManager()->createNewSceneManager();

    ISceneManager *sceneManager = sSceneManager;

    sceneManager->loadScene("data/oldies/kuksa.irr");

    sKuksa = sceneManager->getSceneNodeFromName("kuksa");

    sceneManager->addCameraSceneNode();
    vector3df cameraPosition(0, 5, -10);
    sceneManager->getActiveCamera()->setPosition(cameraPosition);
    sceneManager->getActiveCamera()->setTarget(vector3df(0, 0, 0));

    sKuksa->getMaterial(0).setFlag(video::EMF_LIGHTING, true);
    /*
    assert(sKuksa->getType() == scene::ESNT_MESH || sKuksa->getType() == scene::ESNT_ANIMATED_MESH);
    IMeshSceneNode *aivotMSN = (IMeshSceneNode *)sKuksa;
    sceneManager->getMeshManipulator()->recalculateNormals(aivotMSN->getMesh(), true, true);
    //sceneManager->getMeshManipulator()->setVertexColors(sCube->getMesh(), video::SColor(0xffffffff));
    */

    mInitialized = true;
    return true;
}


void KuksaFx::render(double time, int timeMs)
{
    const float endTransitionLength = (float)(mRenderEndTime - mFxEndTime);
    float fxEndTransitionScale = 1.0f - sBoxStep((float)time, (float)mFxEndTime, (float)mFxEndTime + endTransitionLength);
    sKuksa->setScale(vector3df(fxEndTransitionScale, fxEndTransitionScale, fxEndTransitionScale));
	sSceneManager->drawAll();
}


void KuksaFx::deinit()
{
    sSceneManager->drop();
    mInitialized = false;
}
